﻿using System.Collections;
using UnityEngine;

namespace XLua_Ki
{
    /// 专门处理音乐/音效
    public class AudioBehaviour : MonoBehaviour
    {
        private bool isInit = false;              //是否已设置过音量
        private float? volumnValue;
        public float VolumnValue
        {
            get
            {
                return (float)volumnValue;
            }
            set
            {
                volumnValue = value;
                AudioSource_.volume = value;
            }
        }
        private AudioSource m_audioSource;       //音量大小
        public AudioSource AudioSource_
        {
            get
            {
                if (m_audioSource == null)
                {
                    m_audioSource = GetComponent<AudioSource>();
                }
                return m_audioSource;
            }
            set
            {
                m_audioSource = value;
            }
        }

        private ParticleSystem m_particleSystem;
        public ParticleSystem ParticleSystem_
        {
            get
            {
                if (m_particleSystem == null)
                {
                    m_particleSystem = GetComponent<ParticleSystem>();
                }
                return m_particleSystem;
            }
            set
            {
                m_particleSystem = value;
            }
        }  //特效

        /// 开始播放音效(无特效)
        public void StartPlayVoice(float value, bool isLoop = false, float spatialBlend = 0)
        {
            if (isInit == false || VolumnValue != value)
            {
                SetAudioValue(value, isLoop, spatialBlend);
            }

            StartCoroutine(OnPlayVoice());
        }

        /// 开始播放特效+音效
        public void StartPlayEffect(float value, bool isLoop = false, float spatialBlend = 0)
        {
            if (isInit == false || VolumnValue != value)
            {
                SetAudioValue(value, isLoop, spatialBlend);
            }

            StartCoroutine(OnPlayEffect());
        }

        // 设置音乐的属性
        private void SetAudioValue(float value, bool isLoop, float spatialBlend)
        {
            VolumnValue = value;
            AudioSource_.loop = isLoop;
            AudioSource_.spatialBlend = spatialBlend; //2D音效 = 0 ,  3D音效 = 1
            isInit = true;
        }

        // 播放音效
        IEnumerator OnPlayVoice()
        {
            AudioSource_.SetScheduledStartTime(0);
            if (AudioSource_.isPlaying == false)
            {
                AudioSource_.Play();
            }
            else
            {
                AudioSource_.Stop();
                AudioSource_.Play();
            }

            while (AudioSource_.isPlaying)
            {
                yield return new WaitForSeconds(AudioSource_.clip.length);
            }
            AudioDestroy();
        }

        // 播放特效
        IEnumerator OnPlayEffect()
        {
            ParticleSystem_.Play();
            yield return StartCoroutine(OnPlayVoice());
        }

        private void AudioDestroy()
        {
            StopAllCoroutines();
            GameManager_Ki.Instance.GetPoolManager_Ki().OutOfScene(gameObject);
        }
    }
}
